<?php
namespace Common\Model\Admin;

use Common\Base\ModelBase;

class AdminModel extends ModelBase
{

	/**
     * 得到用户列表
     *
     * @param int    $w
     * @param array  $search
     * @param string $fields
     * @param int    $page
     * @param int    $pageSize
     * @param string $by
     * @param string $exclude
     *
     * @return array
     */
    public function getAdminList($w=3, $search=array(), $fields='a.*,b.group_name', $page=0, $pageSize=0, $by='', $exclude='')
    {
        //查询条件
        $where =  ' where 1 ';
        if( ! empty($search))
        {
            isset($search['admin_id']) && intval($search['admin_id'])>0 && $where .= ' and a.admin_id='.intval($search['admin_id']);
            isset($search['admin_group_id']) && intval($search['admin_group_id'])>0 && $where .= ' and a.admin_group_id='.intval($search['admin_group_id']);
            isset($search['company_id']) && intval($search['company_id'])>0 && $where .= ' and a.company_id='.intval($search['company_id']);
            isset($search['st']) && $search['st']!='' && $where .= " and a.st=".intval($search['st']);
            isset($search['admin_name']) && $search['admin_name']!='' && $where .= " and a.admin_name='".$search['admin_name']."'";
            isset($search['admin_name_like']) && $search['admin_name_like']!='' && $where .= " and a.admin_name like '%".$search['admin_name_like']."%'";
            isset($search['root']) && !$search['root'] && $where .= ' and admin_id!=1';
        }
        if($exclude){ $where .= $exclude; }

        //排序
        $orderBy = ' order by ';
        $by = trim($by);
        if($by == ''){
            $orderBy .= 'a.admin_id desc';
        }else{
            $orderBy .= $by;
        }

        //SQL
        $sql['sql_ct'] = 'select count(a.admin_id) as tot from '.getTable('acl_admin').' as a left join '.getTable('acl_group').' as b on a.admin_group_id=b.group_id '.$where;
        $sql['sql']    = 'select '.$fields.' from '.getTable('acl_admin').' as a left join '.getTable('acl_group').' as b on a.admin_group_id=b.group_id '.$where.$orderBy;

        return $this->getData($w, $sql, $page, $pageSize);
    }

    /**
     * 得到用户列表
     *
     * @param int    $w
     * @param array  $search
     * @param string $fields
     * @param int    $page
     * @param int    $pageSize
     * @param string $by
     * @param string $exclude
     *
     * @return array
     */
    public function getTableAdmin($w=3, $search=array(), $fields='*', $page=0, $pageSize=0, $by='', $exclude='')
    {
        //查询条件
        $where =  ' where 1 ';
        if( ! empty($search))
        {
            isset($search['admin_id']) && intval($search['admin_id'])>0 && $where .= ' and admin_id='.intval($search['admin_id']);
            isset($search['admin_group_id']) && intval($search['admin_group_id'])>0 && $where .= ' and admin_group_id='.intval($search['admin_group_id']);
            isset($search['st']) && $search['st']!='' && $where .= " and st=".intval($search['st']);
            isset($search['admin_name']) && $search['admin_name']!='' && $where .= " and admin_name='".$search['admin_name']."'";
            isset($search['admin_name_like']) && $search['admin_name_like']!='' && $where .= " and admin_name like '%".$search['admin_name_like']."%'";
            isset($search['root']) && !$search['root'] && $where .= ' and admin_id!=1';
            isset($search['group_id']) && intval($search['group_id']) >0 && $where .= ' and admin_group_id ='.intval($search['group_id']);
        }
        if($exclude){ $where .= $exclude; }

        //排序
        $orderBy = ' order by ';
        $by = trim($by);
        if($by == ''){
            $orderBy .= 'admin_id desc';
        }else{
            $orderBy .= $by;
        }

        //SQL
        $sql['sql_ct'] = 'select count(admin_id) as tot from '.getTable('acl_admin').$where;
        $sql['sql']    = 'select '.$fields.' from '.getTable('acl_admin').$where.$orderBy;

        return $this->getData($w, $sql, $page, $pageSize);
    }

    /**
     * 修改用户信息
     *
     * @param array $dat 需要更新的数据
     * @param int   $admin_id
     *
     * @return mixed
     */
    public function updateAdminByAdminId($dat=array(), $admin_id=0)
    {
        return $this->updateRow(array('admin_id'=>$admin_id), $dat, getTable('acl_admin'));
    }

    /**
     * 添加用户
     *
     * @param array $dat
     *
     * @return mixed
     */
    public function addAdmin($dat = array())
    {
        return $this->addRow($dat, getTable('acl_admin'));
    }

}